package bytestream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Date;
public class J_BufferedInputStream
{
    private static String m_fileName = "J_BufferedInputStream.class";

    public static void main(String args[ ])
    {
        try
        {
            int i, ch;
            i = 0;
            Date d1= new Date( );
            FileInputStream f = new FileInputStream(m_fileName);
            while ((ch=f.read( )) != -1) // read entire file
                i++;
            f.close( );
            Date d2= new Date( );

            long t = d2.getTime( ) - d1.getTime( ); // 单位(毫秒)
            System.out.println("读取文件"+m_fileName+"(共"+i+"字节)");
            System.out.println("不带缓存的方法需要"+t+"毫秒");

            i = 0;
            d1= new Date( );
            f = new FileInputStream(m_fileName);
            BufferedInputStream fb = new BufferedInputStream(f);
            while ((ch=fb.read( )) != -1) // read entire file
                i++;
            fb.close( );
            d2= new Date( );

            t = d2.getTime( ) - d1.getTime( ); // 单位(毫秒)
            System.out.println("带缓存的方法需要"+t+"毫秒");
        }
        catch (Exception e)
        {
            System.err.println("发生异常:" + e);
            e.printStackTrace( );
        } // try-catch结构结束
    } // 方法main结束
} // 类J_BufferedInputStream结束
